﻿namespace Hotel.Module.Resources.Models
{
    using System;
    using System.Collections.ObjectModel;
    using Entities = Hotel.Module.Resources.DataRepository;
    using System.Collections.Generic;
    
    public class Hall : Model.Infrastructure.IDistributedStorageEntity
    {
        /// <summary>
        /// Creates a new Instance of Hall according to recived Entity Hall instance
        /// </summary>
        /// <param name="entity">Entity Hall object from which new Hall instance will be created</param>
        public static Hall FromEnityModel(Entities.Hall entity)
        {
            Hall result = new Hall();
            if (entity == null)
            {
                return null;
            }
            else
            {
                result.Number = entity.Number;
                result.State = (State)entity.State;
                result.Type = (HallType)entity.Type;
                   
                foreach (var period in entity.Periods)
                {
                    result.Periods.Add(Period.FromEnityModel(period));
                }
            }
            return result;
        }
        /// <summary>
        /// Default constructor
        /// </summary>
        public Hall()
        {
            m_Periods = new Collection<Period>();
        }

        /// <summary>
        /// Room number
        /// </summary>
        public int Number { get; set; }

        /// <summary>
        /// The current state of the room
        /// </summary>
        public State State { get; set; }

        /// <summary>
        /// Room type
        /// </summary>
        public HallType Type { get; set; } 
        
        /// <summary>
        /// List of periods when room is busy
        /// </summary>
        public Collection<Period> Periods { get { return m_Periods; } set { m_Periods = value; } }
        /// <summary>
        /// all periods to wich current room is bond to
        /// </summary>
        private Collection<Period> m_Periods;
        
        public string PartitionKey{get;set;}

        public string RowKey { get; set; }

        public DateTime Timestamp { get; set; }

        /// <summary>
        /// Assignes to recieved object data from current instnce
        /// </summary>
        /// <param name="obj">Object to which the data of current insctnce will be copied</param>
        public void AssignTo(object obj)
        {
            if (obj is Entities.Hall)
            {
                Entities.Hall result = obj as Entities.Hall;
                
                result.Number = Number;
                result.Type = (int)Type;
                result.State = (int)State;

                List<Entities.Period> EntityPeriods = new List<Entities.Period>();
                foreach (var entityItem in result.Periods)
                {
                    EntityPeriods.Add(entityItem);
                }

                foreach (var item in Periods)
                {
                    if (!EntityPeriods.Exists(p => p.OrderID == item.OrderID))
                    {
                        Entities.Period EntityItem = new Entities.Period();
                        item.AssignTo(EntityItem);
                        result.Periods.Add(EntityItem);
                    }
                }
               
                return;
            }

            throw new NotSupportedException();
        }
       
    }
}
